package com.hspedu.socket;


import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/1
 * @time 20:17
 */
public class SocketTCP01Server {
    public static void main(String[] args) throws IOException {
        //1.创建服务器端监听和接收的套接字
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println(serverSocket.getClass());
        System.out.println("服务端正在监听...");
        //2.利用该套接字进行监听
        Socket newSocket = serverSocket.accept();

        //3. 获取输入流,读取数据
        InputStream inputStream = newSocket.getInputStream();
        int readLength = 0;
        byte[] buffer = new byte[1024];
        while ((readLength = inputStream.read(buffer)) != -1){
            System.out.println(new String(buffer,0,readLength));
        }

        //4. 关闭资源
        inputStream.close();
        newSocket.close();
        serverSocket.close();
        System.out.println("服务器端退出...");
    }
}
